Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Consider the following C function:int f(int n... Start Learning for Free
Consider the following C function:
int f(int n)
{
static int i = 1;
if(n >= 5) return n;
n = n+i;    
i++;
return f(n);
}
The value returned by f(1) is
  • a)
    5
  • b)
    6
  • c)
    7
  • d)
    8
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
Consider the following C function:int f(int n){static int i = 1;if(n &...
answer is 7.as,
f(1):n=2,i=2
f(2):n=4,i=3
f(4):n=7,i=4
f(7):print(n)===>>> 7<ans>
View all questions of this test
Most Upvoted Answer
Consider the following C function:int f(int n){static int i = 1;if(n &...
Explanation:

The given function f(int n) takes an integer n as input and returns an integer value.

The function uses a static variable i which is initialized to 1 and is used in the calculation of the returned value.

The function first checks if the input value n is equal to 5. If it is, then the function returns the value 5.

If the input value n is not equal to 5, then the function multiplies n with the value of i and updates the value of i by incrementing it by 1.

After this, the function calls itself recursively with the updated value of n.

Finally, the function returns the value returned by the recursive call.

Now, let's see what happens when we call the function with the input value 1, i.e. f(1).

- The function checks if the input value n is equal to 5. Since n is not equal to 5, the function multiplies n (which is 1) with the value of i (which is 1) and updates the value of i to 2.
- The function then calls itself recursively with the updated value of n (which is now 1*1 = 1).
- In the next iteration, the function again multiplies n (which is now 1) with the value of i (which is now 2) and updates the value of i to 3.
- The function again calls itself recursively with the updated value of n (which is now 1*2 = 2).
- This process repeats until the value of i becomes 6, at which point the function checks if the input value n is equal to 5. Since n is not equal to 5, the function multiplies n (which is now 2) with the value of i (which is now 6) and updates the value of i to 7.
- The function then calls itself recursively with the updated value of n (which is now 2*6 = 12).
- Since the input value n is not equal to 5, the function continues the recursion until the value of i becomes 11 and the input value n becomes 60 (i.e. 2*3*4*5).
- At this point, the function checks if the input value n is equal to 5. Since it is not, the function multiplies n (which is now 60) with the value of i (which is now 11) and updates the value of i to 12.
- The function then calls itself recursively with the updated value of n (which is now 60*11 = 660).
- Since the input value n is not equal to 5, the function continues the recursion until the value of i becomes 16 and the input value n becomes 277200 (i.e. 2*3*4*5*6*7*8*9*10).
- At this point, the function checks if the input value n is equal to 5. Since it is not, the function multiplies n (which is now 277200) with the value of i (which is now 16) and updates the value of i to 17.
- The function then calls itself recursively with the updated value of n (which is now 277200*16 = 4435200).
- Since the input value n is not equal to 5, the function continues
Free Test
Community Answer
Consider the following C function:int f(int n){static int i = 1;if(n &...
Static i is not created every time you call the function
Explore Courses for Computer Science Engineering (CSE) exam

Similar Computer Science Engineering (CSE) Doubts

Top Courses for Computer Science Engineering (CSE)

Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer?
Question Description
Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer?.
Solutions for Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer?, a detailed solution for Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer? has been provided alongside types of Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following C function:int f(int n){static int i = 1;if(n >= 5) return n;n = n+i; i++;return f(n);}The value returned by f(1) isa)5b)6c)7d)8Correct answer is option 'C'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev